package com.koron.css2.meterService.impl;

import com.github.pagehelper.PageHelper;
import com.koron.css2.baseConfig.mapper.BusinessAreaMapper;
import com.koron.css2.baseConfig.mapper.MeterBoreMapper;
import com.koron.css2.baseConfig.mapper.MeterFactoryMapper;
import com.koron.css2.baseConfig.queryBean.MeterBoreQueryBean;
import com.koron.css2.baseConfig.queryBean.MeterFactoryQueryBean;
import com.koron.css2.baseConfig.vo.MeterBoreVO;
import com.koron.css2.baseConfig.vo.MeterFactoryVO;
import com.koron.css2.common.dto.AttachmentDto;
import com.koron.css2.common.service.AbstractExportExcel;
import com.koron.css2.meterService.bean.query.MsChgPlanDetailQueryBean;
import com.koron.css2.meterService.dto.MsChgPlanDetailDto;
import com.koron.css2.meterService.mapper.MsChgPlanDetailMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.css2.serviceManage.vo.DictionaryVO;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 换表历史导出
 * @author 周利斌
 * com.koron.css2.meterService.impl.MsChgExport
 */
public class MsChgExport extends AbstractExportExcel{
	
	@Override
	public AttachmentDto getAttachment(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		MsChgPlanDetailQueryBean bean = JsonUtils.objectToPojo(req.getData(), MsChgPlanDetailQueryBean.class);
		PageHelper.startPage(bean.getPage(), bean.getPageCount());
		MsChgPlanDetailMapper mapper = factory.getMapper(MsChgPlanDetailMapper.class);	
		List<DictionaryVO> dvo = Tools.listDicByCode(factory, "OMD");
		List<MsChgPlanDetailDto> list = mapper.selectHistoryList(bean);
		MeterBoreMapper meterBoreMapper = factory.getMapper(MeterBoreMapper.class);
		List<MeterBoreVO> meterBores = meterBoreMapper.selectMeterBoreList(new MeterBoreQueryBean());
		
		for(MsChgPlanDetailDto detail : list) {
			// 水表口径
//			if (StringUtils.isNotBlank(detail.getOldMeterBore())) {	//旧水表口径
//				MeterBoreVO meterBore = meterBores.stream().filter(k -> Objects.equals(detail.getOldMeterBore(), k.getId())).findFirst().orElse(new MeterBoreVO());
//				detail.setOldMeterBore(meterBore.getBoreName());
//			}
			
//			if (StringUtils.isNotBlank(detail.getNewMeterBore())) {	//新水表口径
//				MeterBoreVO meterBore = meterBores.stream().filter(k -> Objects.equals(detail.getNewMeterBore(), k.getId())).findFirst().orElse(new MeterBoreVO());
//				detail.setNewMeterBore(meterBore.getBoreName());
//			} 
			// 获取营业区域
			if (!StringUtils.isEmpty(detail.getBusinessArea())) {
				BusinessAreaMapper businessAreaMapper = factory.getMapper(BusinessAreaMapper.class);
				detail.setBusinessArea(
						businessAreaMapper.findBusinessAreaById(detail.getBusinessArea(), userInfo.getCurWaterCode()));
			} 
			MeterFactoryMapper meterFactoryMapper = factory.getMapper(MeterFactoryMapper.class);
			List<MeterFactoryVO> meterFactorys = meterFactoryMapper.selectMeterFactoryList(new MeterFactoryQueryBean());
			
			if (StringUtils.isNotBlank(detail.getMeterFactory())) {
				// 水表厂家
				Optional<MeterFactoryVO> optional = meterFactorys.stream().filter(o -> Objects.equals(o.getId(), detail.getMeterFactory())).findFirst();
				detail.setMeterFactory(optional.orElse(new MeterFactoryVO(detail.getMeterFactory())).getFactoryName());
			}

			
			if(detail.getOldHandleWay() != null) {
				for(DictionaryVO vo: dvo) {
					if(vo.getValue().equals(detail.getOldHandleWay())) {
						detail.setOldHandleWayName(vo.getName());
						break;
					}
				}
			}				
		}
		AttachmentDto attachment = new AttachmentDto();
		attachment.setBtlTemplate("MsChgExport.btl");
		attachment.setFileTemplate("MsChgExport.xlsx");
		attachment.setData(list);
		attachment.setFileType("excel");
		attachment.setFilename("换表历史");
		return attachment;
	}

}
